package com.zegome.game.oanquan;

import com.zegome.game.main.ZegomeActivity;
import com.zegome.secure.PreferenceManager;

/**
 * (C) 2013 ZeroTeam
 * @author QuanLT
 */

public class HighScoreHelper {

	// ===========================================================
	// Constants
	// ===========================================================
	private static final String NAME_DEFAULT = "---";
	private static final int SCORE_DEFAULT = -1;
	
	private static final String NAME_KEY = "top";
	private static final String SCORE_KEY = "score";
	
	public static final int TOP = 10;
	
	// ===========================================================
	// Fields
	// ===========================================================
	
	// ===========================================================
	// Constructor
	// ===========================================================
	
	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public static String[] getNameArray(final ZegomeActivity activity) {
		final String[] results = new String[TOP];
		
		final PreferenceManager preference = activity.getPreference();
		
		for (int i = 0; i < TOP; i++) {
			results[i] = preference.getString(NAME_KEY + i, NAME_DEFAULT);
		}
		
		return results;
	}
	
	public static String getName(final ZegomeActivity activity, final int index) {
		final String name = activity.getPreference().getString(NAME_KEY + index, NAME_DEFAULT);
		return name;
	}
	
	public static int[] getScoreArray(final ZegomeActivity activity) {
		final int[] results = new int[TOP];
		
		final PreferenceManager preference = activity.getPreference();
		
		for (int i = 0; i < TOP; i++) {
			results[i] = preference.getInt(SCORE_KEY + i, SCORE_DEFAULT);
		}
		
		return results;
	}
	
	public static int getScore(final ZegomeActivity activity, final int index) {
		final int score = activity.getPreference().getInt(SCORE_KEY + index, SCORE_DEFAULT);
		return score;
	}
	
	public static int hasHighScore(final ZegomeActivity activity, final int score) {
		final PreferenceManager preference = activity.getPreference();
		
		for (int i = 0; i < TOP; i++) {
			if (preference.getInt(SCORE_KEY + i, SCORE_DEFAULT) < score) {
				return i;
			}
		}
		
		return SCORE_DEFAULT;
	}
	
	public static void savaHighScore(final ZegomeActivity activity, final String name, final int score, final int index) {
		final PreferenceManager preference = activity.getPreference();
		
		final int[] scores = getScoreArray(activity);
		final String[] names = getNameArray(activity);
		
		for (int i = index + 1; i < TOP; i ++) {
			preference.saveValue(NAME_KEY + i, names[i - 1]);
			preference.saveValue(SCORE_KEY + i, scores[i - 1]);
		}
		preference.saveValue(NAME_KEY + index, name);
		preference.saveValue(SCORE_KEY + index, score);
	}
	
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
